package cn.edu.ctbu.jpatest.controller;

import cn.edu.ctbu.jpatest.entity.Student;
import cn.edu.ctbu.jpatest.service.StudentService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author xiaopeng
 * @version 1.0
 */
@Controller
@RequestMapping("/student")
@RequiredArgsConstructor
public class StudentController {
    private final StudentService studentService;

/*
    有@RequiredArgsConstructor可以不写构造函数
    @Autowired
    public StudentController(StudentService studentService) {
        this.studentService = studentService;
    }*/


    /**
     * 使用thymeleaf的模版引擎完成数据显示
     * 返回model模型
     * @param model
     * @return 返回html静态界面路径，模版student/list.html
     */
    @GetMapping("/list")
    public String getList(Model model){
        List<Student> students = studentService.getAllStu();
        model.addAttribute("students",students); //将数据放入model模型中
        return "student/list"; // 返回对应的模版student/list.html
    }

    /**
     * 使用thymeleaf的模版引擎完成数据显示
     * 返回model模型
     * @param model
     * @return 返回html静态界面路径，模版student/StudentManager.html
     */
    @GetMapping("/stuManager")
    public String getView(Model model){
        List<Student> students = studentService.getAllStu();
        model.addAttribute("students",students); //将数据放入model模型中
        return "student/StudentManager"; // 返回对应的模版student/list.html
    }

}
